package com.noname.lib_base_java.util;

import android.view.View;
import android.view.ViewGroup;

import com.noname.lib_base_java.R;

/**
 * Author: Lmoumou
 * Date: 2018-09-28 15:04
 * 文件名: ViewUtils
 * 描述:
 */
public class ViewUtils {

    /**
     * View设置Margin
     * view必须的有父控件，不然无效
     *
     * @param view
     * @param left
     * @param top
     * @param right
     * @param bottom
     */

    public static void setMargin(View view, int left, int top, int right, int bottom) {
        ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
        if (p == null) {
            return;
        }
        int leftCopy = DensityUtil.dp2px(left);
        int topCopy = DensityUtil.dp2px(top);
        int rightCopy = DensityUtil.dp2px(right);
        int bottomCopy = DensityUtil.dp2px(bottom);
        p.setMargins(leftCopy, topCopy, rightCopy, bottomCopy);
        view.requestLayout();
    }


    /**
     * View是否可以第二次点击
     *
     * @param view
     * @param spaceTime
     */
    public static boolean canClick(View view, int spaceTime) {
        Object tag = view.getTag(R.id.tag_double_click);
        Long lastClickTime;

        if (tag != null) {
            lastClickTime = Long.valueOf(tag.toString());
        } else {
            lastClickTime = 0L;
        }

        Long currentTime = System.currentTimeMillis();

        boolean result = currentTime - lastClickTime > spaceTime;

        if (result) {
            view.setTag(R.id.tag_double_click, currentTime);
        }
        return result;
    }

    /**
     * View是否可以第二次点击
     * 间隔时间默认1秒
     *
     * @param view
     */
    public static boolean canClick(View view) {
        return canClick(view, 1000);
    }
}
